package api

import (
	"context"
	"gitee.com/leo-xin/novel_distribution_sdk_go/heiyan/config"
	"net/http"
	"net/url"
)

type OrderApiService service

type OrderRequest struct {
	ctx        context.Context
	ApiService *OrderApiService
	pageNum    *uint64
	pageSize   *uint64
	startDate  *string
	endDate    *string
	site       *int
}

type OrderResponse struct {
	Code     *int64     `json:"code,omitempty"`
	Message  *string    `json:"message,omitempty"`
	Success  *bool      `json:"success,omitempty"`
	Redirect *bool      `json:"redirect,omitempty"`
	Data     *OrderData `json:"data,omitempty"`
}

type OrderData struct {
	PageNum     *string      `json:"pageNum,omitempty"`
	PageSize    *string      `json:"pageSize,omitempty"`
	TotalCount  *string      `json:"totalCount,omitempty"`
	TotalPage   *string      `json:"totalPage,omitempty"`
	HasPrePage  *bool        `json:"hasPrePage,omitempty"`
	HasNextPage *bool        `json:"hasNextPage,omitempty"`
	Items       []*OrderItem `json:"items,omitempty"`
}

type OrderItem struct {
	OrderId          *string `json:"orderId,omitempty"`
	UserId           *string `json:"userId,omitempty"`
	PayMoney         *uint   `json:"payMoney,omitempty"`
	CreateTime       *string `json:"createTime,omitempty"`
	PayTime          *string `json:"payTime,omitempty"`
	PayStatus        *int    `json:"payStatus,omitempty"`
	BookId           *string `json:"bookId,omitempty"`
	LinkId           *string `json:"linkId,omitempty"`
	Group            *int    `json:"group,omitempty"`
	GroupDesc        *string `json:"groupDesc,omitempty"`
	Site             *int    `json:"site,omitempty"`
	SiteDesc         *string `json:"siteDesc,omitempty"`
	Ip               *string `json:"ip,omitempty"`
	Ua               *string `json:"ua,omitempty"`
	UserRegisterDate *string `json:"userRegisterDate,omitempty"`
	RebindingDate    *string `json:"rebindingDate,omitempty"`
	ChannelTypeValue *int    `json:"channelTypeValue,omitempty"`
	ChannelTypeDesc  *string `json:"channelTypeDesc,omitempty"`
	AdParams         *string `json:"adParams,omitempty"`
}

type AdParams struct {
	ClickId      *string `json:"clickid,omitempty"`
	PromotionId  *string `json:"promotionid,omitempty"`
	ProjectId    *string `json:"projectid,omitempty"`
	Callback     *string `json:"callback,omitempty"`
	ClickId2     *string `json:"click_id,omitempty"`
	TrackId      *string `json:"trackid,omitempty"`
	ClueToken    *string `json:"clue_token,omitempty"`
	PromotionId2 *string `json:"promotion_id,omitempty"`
	ProjectId2   *string `json:"project_id,omitempty"`
	AdvertiserId *string `json:"advertiser_id,omitempty"`
	BdVid        *string `json:"bd_vid,omitempty"`
	Imp          *string `json:"imp,omitempty"`
	AppId        *string `json:"app_id,omitempty"`
	OpenId       *string `json:"open_id,omitempty"`
	UnionId      *string `json:"union_id,omitempty"`
	Ip           *string `json:"ip,omitempty"`
	Ua           *string `json:"ua,omitempty"`
}

func (r *OrderRequest) Pid(pid string) *OrderRequest {
	r.ctx = context.WithValue(r.ctx, config.PId, pid)
	return r
}

func (r *OrderRequest) Secret(secret string) *OrderRequest {
	r.ctx = context.WithValue(r.ctx, config.Secret, secret)
	return r
}

func (r *OrderRequest) PageNum(pageNum uint64) *OrderRequest {
	r.pageNum = &pageNum
	return r
}

func (r *OrderRequest) PageSize(pageSize uint64) *OrderRequest {
	r.pageSize = &pageSize
	return r
}

func (r *OrderRequest) StartDate(startDate string) *OrderRequest {
	r.startDate = &startDate
	return r
}

func (r *OrderRequest) EndDate(endDate string) *OrderRequest {
	r.endDate = &endDate
	return r
}

func (r *OrderRequest) Site(site int) *OrderRequest {
	r.site = &site
	return r
}

func (r *OrderRequest) Execute() (*OrderResponse, *http.Response, error) {
	return r.ApiService.getExecute(r)
}

func (r *OrderRequest) WithLog(enable bool) *OrderRequest {
	if enable {
		r.ctx = context.WithValue(r.ctx, config.ContextEnableLog, true)
	}
	return r
}

func (a *OrderApiService) Get(ctx context.Context) *OrderRequest {
	return &OrderRequest{
		ApiService: a,
		ctx:        ctx,
	}
}

func (a *OrderApiService) getExecute(r *OrderRequest) (*OrderResponse, *http.Response, error) {
	var (
		localVarPath         = a.client.Cfg.GetBasePath() + "/spider/distribute/orders"
		localVarHTTPMethod   = http.MethodGet
		localVarPostBody     map[string]string
		localVarFormParams   = url.Values{}
		localVarHeaderParams = make(map[string]string)
		localVarQueryParams  = url.Values{}
		localVarReturnValue  *OrderResponse
	)

	r.ctx = a.client.prepareCtx(r.ctx)

	if ok, GenericOpenAPIError := hasReqParam(r.pageNum, "PageNum"); !ok {
		return localVarReturnValue, nil, GenericOpenAPIError
	}
	if ok, GenericOpenAPIError := hasReqParam(r.pageSize, "PageSize"); !ok {
		return localVarReturnValue, nil, GenericOpenAPIError
	}
	localVarQueryParams.Add("pageNum", toString(r.pageNum))
	localVarQueryParams.Add("pageSize", toString(r.pageSize))

	if r.startDate != nil {
		localVarQueryParams.Add("startDate", *r.startDate)
	}

	if r.startDate != nil {
		localVarQueryParams.Add("endDate", *r.endDate)
	}

	if r.site != nil {
		localVarQueryParams.Add("site", toString(r.site))
	}

	req, err := a.client.buildRequest(r.ctx, localVarPath, localVarHTTPMethod, localVarPostBody, localVarHeaderParams, localVarQueryParams, localVarFormParams)
	if err != nil {
		return localVarReturnValue, nil, err
	}

	localVarHTTPResponse, err := a.client.call(r.ctx, req, &localVarReturnValue)
	if err != nil || localVarHTTPResponse == nil {
		return localVarReturnValue, localVarHTTPResponse, err
	}
	return localVarReturnValue, localVarHTTPResponse, nil
}
